let currentAreaNode;

export function addGoInside(districtExplorer, map, now_adcode,big_zoom) {

    //feature被点击
    districtExplorer.on('featureClick', function (e, feature) {

        let props = feature.properties;
        switchAreaNode(props.adcode);
        if (props.childrenNum === 0) {
            map.setZoom(big_zoom);
            map.setCenter(props.center);
        }

    });

    // -----------
    districtExplorer.on('outsideClick', (e) => {

        districtExplorer.locatePosition(e.originalEvent.lnglat, function (error, routeFeatures) {

            let prop = routeFeatures[1].properties;

            if (routeFeatures && routeFeatures.length > 1 && prop.adcode === now_adcode) {
                //切换到省级区域
                switchAreaNode(routeFeatures[2].properties.adcode);
            } else {
                //切换到全国
                switchAreaNode(now_adcode);
            }

        });

    });

    function switchAreaNode(adcode, callback) {

        if (currentAreaNode && ('' + currentAreaNode.getAdcode() === '' + adcode)) {
            return;
        }

        loadAreaNode(adcode, function (error, areaNode) {

            if (error) {

                if (callback) {
                    callback(error);
                }

                return;
            }

            currentAreaNode = areaNode;

            //设置当前使用的定位用节点
            districtExplorer.setAreaNodesForLocating([currentAreaNode]);

            refreshAreaNode(areaNode);

            if (callback) {
                callback(null, areaNode);
            }
        });
    }


    function loadAreaNode(adcode, callback) {

        districtExplorer.loadAreaNode(adcode, function (error, areaNode) {

            if (error) {

                if (callback) {
                    callback(error);
                }

                return;
            }

            if (callback) {
                callback(null, areaNode);
            }
        });
    }


    function refreshAreaNode(areaNode) {

        districtExplorer.setHoverFeature(null);

        renderAreaPolygons(areaNode);

        //更新选中节点的class
        let $nodeEles = $('#area-tree').find('h2');

        $nodeEles.removeClass('selected');

        let $selectedNode = $nodeEles.filter('h2[data-adcode=' + areaNode.getAdcode() + ']').addClass('selected');

        //展开下层节点
        $selectedNode.closest('li').removeClass('hide-sub');

        //折叠下层的子节点
        $selectedNode.siblings('ul.sublist').children().addClass('hide-sub');
    }


    function renderAreaPolygons(areaNode) {
        //更新地图视野
        map.setBounds(areaNode.getBounds(), null, null, true);
        // map.setZoom(big_zoom);
        // map.setZoom(8);
        //清除已有的绘制内容
        districtExplorer.clearFeaturePolygons();

        //绘制子区域
        districtExplorer.renderSubFeatures(areaNode, function () {

            // var fillColor = colors[i % colors.length];
            // var strokeColor = colors[colors.length - 1 - i % colors.length];
            var strokeColor = '#FFFFFF';

            return {
                cursor: 'default',
                bubble: true,
                strokeColor: strokeColor, //线颜色
                strokeOpacity: 1, //线透明度
                strokeWeight: 1, //线宽
                fillColor: null, //填充色
                fillOpacity: 0.35, //填充透明度
            };
        });

        //绘制父区域
        districtExplorer.renderParentFeature(areaNode, {
            cursor: 'default',
            bubble: true,
            strokeColor: '#FFFFFF', //线颜色
            strokeOpacity: 1, //线透明度
            strokeWeight: 1, //线宽
            fillColor: null, //填充色
            fillOpacity: 0.35, //填充透明度
        });
    }
}
